package com.powroznik.tracker.engine.util;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import com.powroznik.tracker.engine.data.DeviceConfiguration;

@Produces("application/json")
@Provider
public class DeviceConfigurationMessageBodyWriter implements
		MessageBodyWriter<DeviceConfiguration> {
	@Override
	public long getSize(DeviceConfiguration ceviceConfiguration, Class<?> c,
			Type t, Annotation[] as, MediaType mt) {
		return -1;
	}

	@Override
	public boolean isWriteable(Class<?> c, Type t, Annotation[] as, MediaType mt) {
		return DeviceConfiguration.class.isAssignableFrom(c);
	}

	@Override
	public void writeTo(DeviceConfiguration deviceConfiguration, Class<?> c,
			Type t, Annotation[] as, MediaType mt,
			MultivaluedMap<String, Object> mm, OutputStream os)
			throws IOException, WebApplicationException {
		try {
			ConversionUtils.writeString(os, ConversionUtils
					.writeDeviceConfiguration(deviceConfiguration));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
